package com.ces.knacky.restws.client;

import java.io.ByteArrayInputStream;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.webdav.lib.methods.PostMethod;




public class TestCatalogRService {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		/*final String addItemXML = "<item>" +
		"<description>item14</description>" +
		"<createTime>2004-10-19T10:23:54+05:30</createTime>" +
		"<updateTime>2004-10-19T10:23:54+05:30</updateTime>" +
		"<user>" +
		"<id>1</id>" +
		"</user>" +
		"<company>" +
		"<id>1</id>" +
		"</company>" +
		"<expectedPrice>10.0</expectedPrice>" +
		"<title>item14</title>"+
		"<flag><id>1</id></flag>" +
		"<itemWorkflowStatus><id>1</id></itemWorkflowStatus>" +
		"<code>item14Code</code>" +
		"</item>";  */
		
		//update xml
		final String addItemXML = "<item>" +
		"<id>1</id>" +
		"<description>item14_upd</description>" +
		"<createTime>2004-10-19T10:23:54+05:30</createTime>" +
		"<updateTime>2004-10-19T10:23:54+05:30</updateTime>" +
		"<user>" +
		"<id>1</id>" +
		"</user>" +
		"<company>" +
		"<id>1</id>" +
		"</company>" +
		"<expectedPrice>10.0</expectedPrice>" +
		"<title>item14_upd</title>"+
		"<flag><id>1</id></flag>" +
		"<itemWorkflowStatus><id>1</id></itemWorkflowStatus>" +
		"<code>item14Code</code>" +
		"</item>";  
		
		//final String addItemJSON = "{\"item\":[{\"desc\":\"Item1\",\"id\":\"1\",\"name\":\"Item1\",\"price\":\"10.0\"}]}";
		//final String addItemJSON = "{\"item\":{\"desc\":\"Item1\",\"id\":\"1\",\"name\":\"Item1\",\"price\":\"10.0\"}}";
		//final String addItemJSON = "{\"desc\":\"Item1\",\"id\":\"1\",\"name\":\"Item1\",\"price\":\"10.0\"}";
		//add json
		//final String addItemJSON = "{\"description\":\"item5\",\"createTime\":\"2004-10-19T10:23:54+05:30\",\"updateTime\":\"2004-10-19T10:23:54+05:30\",\"user\":{\"id\":\"1\"}, \"flag\":{\"id\":\"1\"},\"itemWorkflowStatus\":{\"id\":\"1\"},\"company\":{\"id\":\"1\"},\"expectedPrice\":\"50.0\",\"title\":\"item5\",\"code\":\"item5Code\"}";
		//update json
		final String addItemJSON = "{\"id\":\"2\",\"description\":\"item5_upd\",\"createTime\":\"2004-10-19T10:23:54+05:30\",\"updateTime\":\"2004-10-19T10:23:54+05:30\",\"user\":{\"id\":\"1\"}, \"flag\":{\"id\":\"1\"},\"itemWorkflowStatus\":{\"id\":\"1\"},\"company\":{\"id\":\"1\"},\"expectedPrice\":\"50.0\",\"title\":\"item5_upd\",\"code\":\"item5Code\"}";
		//System.out.println(addItemJSON);
		
		
		
		
				

			   GetMethod getMethod = new GetMethod("http://localhost:8080/eAuction/rest/catalogService");
			   
			   GetMethod getMethod1 = new GetMethod("http://localhost:8080/eAuction/rest/catalogService/14");
		
			   //Add Json
			   PostMethod postMethod = new PostMethod("http://localhost:8080/eAuction/rest/catalogService/add");
			   //Add XML
			   PostMethod postMethod1 = new PostMethod("http://localhost:8080/eAuction/rest/catalogService/addx");
			   
			   PutMethod putMethod = new PutMethod("http://localhost:8080/eAuction/rest/catalogService/update");
			   
			   DeleteMethod deleteMethod = new DeleteMethod("http://localhost:8080/eAuction/rest/catalogService/31");

			   RequestEntity entity = new InputStreamRequestEntity (  

			      new ByteArrayInputStream(addItemXML.getBytes()),  

			       "application/xml");  
			   
			   RequestEntity entityJson = new InputStreamRequestEntity (  

					      new ByteArrayInputStream(addItemJSON.getBytes()),  

					       "application/json");  


			     

			   HttpClient client = new HttpClient();  

			  try {  
				  int result;
				  //Retrieve All
				  //getMethod.setRequestHeader("Accept", "application/json");
				  //result = client.executeMethod(getMethod);
				  //System.out.println(getMethod.getResponseBodyAsString(4000));
				  
				  //Retrieve with id
				  //getMethod1.setRequestHeader("Accept", "application/json");
				  //result = client.executeMethod(getMethod1);
				  //System.out.println(getMethod1.getResponseBodyAsString(4000));
				  
			      //Add XML
				  
				 /* postMethod1.setRequestEntity(entity);
				  result = client.executeMethod(postMethod1);  

			     System.out.println("Response status code: " + result);  

			      System.out.println("Response headers:");  

			     Header[] headers = postMethod1.getResponseHeaders();  

			      for (int i = 0; i < headers.length; i++) 
			      {  

			    	  System.out.println(headers[i].toString());  

			      }
			      System.out.println("XML: " + postMethod1.getResponseBodyAsString(4000));*/
			      
			   //Add JSON
			      
			    
			      
				  /*postMethod.setRequestHeader("Content-Type", "application/xml");
				  postMethod.setRequestEntity(entity);
				  //postMethod.setRequestEntity(entityJson);
			      
			      //postMethod.setRequestEntity(new StringRequestEntity(addItemJSON, "application/json", null));
				  
				 
				  //postMethod.setRequestHeader("Content-Type", "application/json");
				  //postMethod.setRequestHeader("Accept", "application/json");
				  
				  result = client.executeMethod(postMethod);
				  
				  System.out.println(postMethod.getResponseBodyAsString(2000));*/
			      
			  //Update
				  
			  //putMethod.setRequestHeader("Content-Type", "application/xml");
			  putMethod.setRequestEntity(entityJson);
			  result = client.executeMethod(putMethod);
			  System.out.println(putMethod.getResponseBodyAsString(8000));
			  
			  //Delete
			  
			  /*result = client.executeMethod(deleteMethod);
			  System.out.println(deleteMethod.getResponseBodyAsString(100));*/
			  }
			  catch(Exception ex)
			  {
				  ex.printStackTrace();
			  }
			  finally {  

			     postMethod.releaseConnection();  

			  }  


	

}
	
}
